#include <iostream>
#include <unordered_map>

using namespace std;

const int N = 2e5 + 10;

struct Query {
    int x, y, e;
}query[N / 2];

int n, m;
int p[N];
unordered_map<int, int> s;

int get(int x) {
    if (!s.count(x)) s[x] = ++ m;
    return s[x];
}

int find(int x) {
    if (p[x] != x) p[x] = find(p[x]);
    return p[x];
}

int main() {
    int t;
    scanf("%d", &t);
    
    while (t -- ) {
        scanf("%d", &n);
        m = 0;
        s.clear();
        for (int i = 0; i < n; ++ i) {
            int a, b, c;
            scanf("%d%d%d", &a, &b, &c);
            query[i] = {get(a), get(b), c};
        }
        
       for (int i = 1; i <= m; ++ i) p[i] = i;
       
       for (int i = 0; i < n; ++ i)
           if (query[i].e) p[find(query[i].x)] = find(query[i].y);
        
        bool success = true;
        for (int i = 0; i < n; ++ i)
            if (!query[i].e)
                if (find(query[i].x) == find(query[i].y)) {
                    success = false;
                    break;
                }
        
        if (success) puts("YES");
        else puts("NO");
    }
    
    return 0;
}